package cn.caojun.org.controller;

import cn.caojun.auth.domain.Permission;
import cn.caojun.auth.domain.Role;
import cn.caojun.auth.dto.EmployeeRoleDTO;
import cn.caojun.auth.dto.RolePermissionDTO;
import cn.caojun.basic.utils.AjaxResult;
import cn.caojun.basic.utils.PageList;
import cn.caojun.org.domain.Department;
import cn.caojun.org.domain.Employee;
import cn.caojun.org.query.EmployeeQuery;
import cn.caojun.org.service.IEmployeeService;
import cn.caojun.org.service.impl.EmployeeServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try{
            if (Objects.isNull(employee.getId())){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(true).setMessage(e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id")Long id){
        try {
             employeeService.removeById(id);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public AjaxResult getById( @PathVariable("id")Long id){
        try{
            Employee employee=employeeService.getById(id);
            return AjaxResult.me().setObject(employee);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping
    public AjaxResult getAll(){
        try{
            List<Employee> employees=employeeService.getAll();
            return AjaxResult.me().setObject(employees);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping
    public AjaxResult getEmployeeList(@RequestBody EmployeeQuery employeeQuery){

        try{
            PageList<Employee> pageList=employeeService.getLimteList(employeeQuery);
            return AjaxResult.me().setObject(pageList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult pathDelete(@RequestBody List<Long> ids){

        try{
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/role")
    public AjaxResult getRoleTree(){
        try{
            List<Role> roleList=employeeService.getRoleTree();
            return AjaxResult.me().setObject(roleList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PutMapping("/role")
    public AjaxResult setROle(@RequestBody EmployeeRoleDTO employeeRoleDTO){
        try{
            employeeService.setEmployeeRole(employeeRoleDTO);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("角色分配失败"+e.getMessage());
        }
    }

    @GetMapping("/roleId/{roleId}")
    public AjaxResult getAllRoleById(@PathVariable("roleId") Long roleId){
        try{
            List<String> roleMenuList=employeeService.getRoleById(roleId);
            return AjaxResult.me().setObject(roleMenuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
